/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.security;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @ClassName: TokenPoolImpl
 * @Function: TODO
 * @Date: 2020/11/21 23:13
 * @author wyl
 * @version V1.0
 */
@Order(-5)
@ConditionalOnClass(TokenPool.class)
@ConditionalOnMissingBean(TokenPool.class)
@Component
public class TokenPoolImpl implements TokenPool {
    static HashMap<String, String> tokenPool = new HashMap<>();

    /**
     * @Description 放入token token
     * @param userId
     * @param token
     * @return void
     * @Date 2020/11/21 23:07
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void addToken(String userId, String token) {
        tokenPool.put(userId, token);
    }

    /**
     * @Description 判断token是否存在
     * @param userId
     * @param token
     * @return java.lang.Boolean
     * @Date 2020/11/21 23:07
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public Boolean hasToken(String userId, String token) {
        return token.equals(tokenPool.get(userId));
    }

    /**
     * @Description
     * @param userId
     * @param token
     * @return void
     * @Date 2020/11/21 23:10
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void updateToken(String userId, String token) {
        tokenPool.put(userId, token);
    }

    /**
     * @Description 删除token
     * @param userId
     * @return void
     * @Date 2020/11/21 23:12
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void deleteToken(String userId) {
        tokenPool.remove(userId);
    }

    /**
     * @Description 清空token
     * @return void
     * @Date 2020/11/21 23:12
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void cleanTokenPool() {
        tokenPool.clear();
    }
}
